package personal.wang.message;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;

/**
 * @className: MessageEncoder
 * @Description:
 * @Author: wangyifei
 * @Date: 2024/6/4 11:11
 */
public class MessageEncoder extends MessageToByteEncoder<Message> {
    private static Logger logger = LoggerFactory.getLogger(MessageEncoder.class);

    @Override
    protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out) throws Exception {
        Header header = msg.getHeader();
        out.writeInt(header.getVersion());
        out.writeInt(header.getLength());
        out.writeInt(header.getContentLength());
        out.writeInt(header.getFlag());
        out.writeBytes(header.getSessiongId().getBytes(StandardCharsets.UTF_8));
        out.writeBytes(msg.getContent());
    }
}
